package com.thrsdk.common.network;

import android.content.Context;

import com.thrsdk.common.network.converter.GsonConverterFactory;
import com.thrsdk.constants.ApiConstants;
import com.thrsdk.log.LogUtils;
import com.thrsdk.mylibrary.BuildConfig;
import com.thrsdk.utils.GsonUtils;

import java.io.File;
import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import af.okhttp3.OkHttpClient;
import dy.retrofit2.Retrofit;


public class BaseRetrofitClient {

    private static Retrofit retrofit;
    private static int TIME_OUT = 30; // 30秒超时断开连接
    private static Context mcontext;

    private BaseRetrofitClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.baseHttp)
                    .client(newOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create(GsonUtils.getGson()))
                    .build();
        }
    }

    // 静态内部类模式实现单例
    private static class SingleTonHoler {
        private static BaseRetrofitClient INSTANCE = new BaseRetrofitClient();
    }


    public static BaseRetrofitClient getInstance(Context context) {
        mcontext = context;
        return SingleTonHoler.INSTANCE;
    }


    // 自定义一个信任所有证书的TrustManager，添加SSLSocketFactory的时候要用到
    private static X509TrustManager trustAllCert = new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }
    };
    private static SSLSocketFactory sslSocketFactory = new SSLSocketFactoryCompat(trustAllCert);

    // httpclient
    public static OkHttpClient client = new OkHttpClient.Builder()
//            .sslSocketFactory(sslSocketFactory, trustAllCert)
            .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
            .readTimeout(TIME_OUT, TimeUnit.SECONDS)
            .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
            .build();

    public Retrofit getDefaultRetrofit() {
        return retrofit;
    }

    private OkHttpClient newOkHttpClient() {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        okHttpClientBuilder.sslSocketFactory(sslSocketFactory, trustAllCert);
        okHttpClientBuilder.connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS);

        if (null == mcontext) {
            return okHttpClientBuilder.build();
        }
        //添加日志输出
        File appFile = new File(mcontext.getExternalFilesDir(""), "dy.ini");
        boolean debugFileExists = appFile.exists();

        if (BuildConfig.DEBUG || debugFileExists) {
            LogUtils.getInstance().openDebug(true);
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            okHttpClientBuilder.addInterceptor(loggingInterceptor);
        }

        return okHttpClientBuilder.build();
    }


}
